<template>
  <div class="main_box" style="padding:10px;">
    <div class="add_wrap">
      <div class="tit">
       <h2>{{$route.query.id?"修 改 康 来 子 项 目 信 息":"新 增 康 来 子 项 目"}}</h2>
        <el-button
          type="success"
          plain
          size="small"
          icon="fa fa-mail-forward"
          @click="returnList"
        >
          返 回</el-button
        >
      </div>
      <el-form
        :model="dataForm"
        ref="dataForm"
        label-width="200px"
        :rules="roleRules"
      >
        <el-row>
          <el-form-item label="子项目名称" prop="subItemName">
            <el-input v-model="dataForm.subItemName" clearable></el-input>
          </el-form-item>
          <el-col :span="24">
            <el-form-item label="子项目别称" prop="subItemShortName">
            <el-input v-model="dataForm.subItemShortName" clearable></el-input>
          </el-form-item>
          </el-col>

          <!-- <el-form-item label="项目编号" prop="subItemCode">
            <el-input v-model="dataForm.subItemCode"></el-input>
          </el-form-item> -->
          <el-form-item label="收费价格" prop="subItemPrice">
            <el-input type="number" v-model="dataForm.subItemPrice"></el-input>
          </el-form-item>
          <el-col :span="24">
              <el-form-item label="临床意义" prop="subItemMeaning" clearable>
              <el-input
                type="textarea"
                width="100%"
                v-model="dataForm.subItemMeaning"
                placeholder="请输入临床意义"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="注意事项" prop="subItemNote" clearable>
            <el-input
              type="textarea"
              width="100%"
              v-model="dataForm.subItemNote"
              placeholder="请输入注意事项"
            ></el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="备注" prop="remark" clearable>
            <el-input
              type="textarea"
              width="100%"
              v-model="dataForm.remark"
            ></el-input>
          </el-form-item>
          </el-col>

        </el-row>

        <el-row>
          <el-form-item>
            <el-button type="primary" @click="sureClick">保 存</el-button>
            <el-button @click="returnList">取 消</el-button>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import * as message from "@/constants/message";
import ReportItemApi from "@/api/report/reportItem";

export default {
  data() {
    return {
      disabled: false,
      dataForm: {
        // subItemCode: '',
        subItemName: "",
        subItemShortName: "",
        subItemPrice: "",
        subItemNote: "",
        subItemMeaning: "",
        remark:"",
      },
      roleRules: {
        subItemName: [
          {
            required: true,
            message: "请输入项目名称",
            trigger: "blur"
          }
        ]
      }
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    init() {
      let _this = this;
      this.$Utils.clearData(this.dataForm);
      if (this.$route.query.id) {
        _this.dataForm.subItemId = this.$route.query.id;
        _this.getData();
      }
    },
    sureClick() {
      let _this = this;
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          if (_this.dataForm.subItemId) {
            //做一个基础的判断，id不为空就编辑，id为空就增加
            _this
              .$confirm(
                message.MSG_CONFIRM_SAVE_ALTER,
                message.TITLE_PROMPT,
                {}
              )
              .then(() => {
                this.editData();
              });
          } else {
            _this
              .$confirm(message.MSG_CONFIRM_SAVE_ADD, message.TITLE_PROMPT, {})
              .then(() => {
                this.addData();
              });
          }
        }
      });
    },
    addData() {
      let _this = this;
      let cond = {
        // subItemCode: _this.dataForm.subItemCode,
        subItemName: _this.dataForm.subItemName,
        subItemShortName: _this.dataForm.subItemShortName,
        subItemPrice: _this.dataForm.subItemPrice,
        subItemNote: _this.dataForm.subItemNote,
        subItemMeaning: _this.dataForm.subItemMeaning,
        remark:_this.dataForm.remark
      };
      ReportItemApi.addReportSubItem(cond).then(data => {
        this.$notify.success({
          title: message.TITLE_SUCCESS,
          message: message.MSG_ACTION_SUCCESS
        });
        this.returnList();
      })
    },
    editData() {
      let _this = this;
      let cond = {
        subItemId: _this.dataForm.subItemId,
        // subItemCode: _this.dataForm.subItemCode,
        subItemName: _this.dataForm.subItemName,
        subItemShortName: _this.dataForm.subItemShortName,
        subItemPrice: _this.dataForm.subItemPrice,
        subItemNote: _this.dataForm.subItemNote,
        subItemMeaning: _this.dataForm.subItemMeaning,
        remark:_this.dataForm.remark,
      };
      ReportItemApi.editReportSubItem(cond).then(data => {
        this.$notify.success({
          title: message.TITLE_SUCCESS,
          message: message.MSG_ALTER_SUCCESS
        })
        this.returnList();
      }).catch(err=>{
           this.$confirm('该项目与医院组合项目存在关联, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          cond.update = 1
          ReportItemApi.editReportSubItem(cond).then(data => {
              this.$notify.success({
                title: message.TITLE_SUCCESS,
                message: message.MSG_ACTION_SUCCESS
              })
              this.returnList();
            })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
        });;
    },
    getData() {
      ReportItemApi.getReportSubItem({ subItemId: this.$route.query.id }).then(
        res => {
          // console.log(res.data)
          this.$Utils.bindingBean(this.dataForm, res.data);
          if (res.data.subItemPrice) {
            this.dataForm.subItemPrice = parseInt(res.data.subItemPrice);
          }
        }
      );
    },
    returnList() {
      //   this.$router.push({path: '/report/itemManagement'})
      // this.$router.go(-1)
      this.$store.dispatch("delView", this.$route);
      this.$router.push({ path: "/item/reportSubItemManagement" });
    }
  }
};
</script>
<style lang="scss" src="@/assets/css/views/add.scss" scope></style>
<style scoped>
.add_wrap {
  padding-top: 10px;
  padding-left: 23px;
}
</style>
